package cn.itsource.blog.controller;

import cn.itsource.blog.domain.Param;
import cn.itsource.blog.query.ParamQuery;
import cn.itsource.blog.service.IParamService;
import cn.itsource.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * 接口类：博客参数Param
 */
@RestController
@RequestMapping("/param")
@Api(tags = "博客参数接口类")
public class ParamController {
    @Autowired
    private IParamService paramService;

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<Param> findAll(){
        return paramService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Param findOne(@PathVariable("id")Long id){
        return paramService.findOne(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id){
        try {
            paramService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 添加或修改
     * @param param
     * @return
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Param param){
        try {
           if(param.getId()==null){
               paramService.add(param);
           }else{
               paramService.update(param);
           }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDel(@RequestBody Long[] ids){
        try {
            paramService.patchDel(ids);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     * 分页查询+高级查询
     * @param paramQuery
     * @return
     */
    @PostMapping
    public PageInfo<Param> queryPage(@RequestBody ParamQuery paramQuery){
        return paramService.queryPage(paramQuery);
    }


    @GetMapping("/getParams")
    public Map<String,String> getParams(){
        return paramService.getParams();
    }
}
